package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.constant.MallConstants;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.utils.bean.BeanUtils;
import com.tencreat.domain.mall.MallAppCategory;
import com.tencreat.domain.mall.MallCategory;
import com.tencreat.domain.mall.vo.MallCategoryVO;
import com.tencreat.mapper.mall.MallAppCategoryMapper;
import com.tencreat.mapper.mall.MallCategoryMapper;
import com.tencreat.mapper.mall.MallGoodsMapper;
import com.tencreat.mapper.mall.MallStoreGoodsMapper;
import com.tencreat.service.mall.IMallAppCategoryService;
import com.tencreat.service.mall.IMallCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * APP类目Service业务层处理
 *
 * @author tencreat
 * @date 2021-08-13
 */
@Service
public class MallAppCategoryServiceImpl extends ServiceImpl<MallAppCategoryMapper, MallAppCategory> implements IMallAppCategoryService {

    //@Autowired
    private MallStoreGoodsMapper mallStoreGoodsMapper;
    /**
     * 查询类目
     *
     * @param id 类目ID
     * @return 类目
     */
    @Override
    public MallAppCategory selectMallAppCategoryById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询类目列表
     *
     * @param mallAppCategory 类目
     * @return 类目
     */
    @Override
    public List<MallAppCategory> selectMallAppCategoryList(MallAppCategory mallAppCategory) {
        return baseMapper.selectList(Wrappers.query(mallAppCategory));
    }

    /**
     * 描述信息  新增店铺商品类目
     * @Author yaoruiqi
     * @Date: 2023/10/12 03:48
     */
    @Override
    public int insertMallAppCategory(MallAppCategory mallAppCategory) {
        Integer count = null;
        mallAppCategory.setCreateTime(LocalDateTime.now());
/*        if (MallConstants.FOODS.equals(mallAppCategory.getLevel())) {
            MallAppCategory parent = baseMapper.selectById(mallAppCategory.getPid());
            if (!parent.getHasChildren()) {
                parent.setHasChildren(true);
                baseMapper.updateById(parent);
            }
            count  = lambdaQuery().eq(MallAppCategory::getLevel, MallConstants.FOODS).eq(MallAppCategory::getPid,mallAppCategory.getPid()).count();
        }
        if (MallConstants.MEDICAL.equals(mallAppCategory.getLevel())) {
            MallAppCategory parent = baseMapper.selectById(mallAppCategory.getPid());
            if (!parent.getHasChildren()) {
                parent.setHasChildren(true);
                baseMapper.updateById(parent);
            }
            count  = lambdaQuery().eq(MallAppCategory::getLevel, MallConstants.MEDICAL).eq(MallAppCategory::getPid,mallAppCategory.getPid()).count();
        }*/
        if (MallConstants.FOODS.equals(mallAppCategory.getLevel())){
            mallAppCategory.setType(0L);
            count =lambdaQuery().eq(MallAppCategory::getLevel, MallConstants.FOODS).count();
        }
        if (MallConstants.MEDICAL.equals(mallAppCategory.getLevel())){
            mallAppCategory.setType(1L);
            count =lambdaQuery().eq(MallAppCategory::getLevel, MallConstants.MEDICAL).count();
        }
        mallAppCategory.setSortOrder(count);
        mallAppCategory.setStatus(true);
        return baseMapper.insert(mallAppCategory);
    }

    /**
     * 修改类目
     * @param mallAppCategory 类目
     * @return 结果
     */
    @Override
    public int updateMallAppCategory(MallAppCategory mallAppCategory) {
        //修改二级类目的父类目ID，需要修改是否有子节点字段
        MallAppCategory category = baseMapper.selectById(mallAppCategory.getId());
     /*   if(!category.getPid().equals(mallAppCategory.getPid())){
            Integer count = lambdaQuery().eq(MallAppCategory::getPid, category.getPid()).count();
            if(count<2){
                MallAppCategory pCategory = baseMapper.selectById(category.getPid());
                pCategory.setHasChildren(false);
                baseMapper.updateById(pCategory);
            }
            MallAppCategory newPcategory = baseMapper.selectById(mallAppCategory.getPid());
            newPcategory.setHasChildren(true);
            baseMapper.updateById(newPcategory);
        }
        //下架
        if(mallAppCategory.getStatus().equals(false) && mallAppCategory.getLevel().equals("L1")){
            for (MallAppCategory mallAppCategory1 : lambdaQuery().eq(MallAppCategory::getPid, mallAppCategory.getId()).list()) {
                lambdaUpdate().set(MallAppCategory::getStatus,"0").eq(MallAppCategory::getId,mallAppCategory1.getId()).update();
                mallStoreGoodsMapper.updateIsOnSaleByCategory("0",mallAppCategory1.getId());
            }
        }*/
        mallAppCategory.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallAppCategory);
    }

    /**
     * 批量删除类目
     *
     * @param ids 需要删除的类目ID
     * @return 结果
     */
    @Override
    public int deleteMallAppCategoryByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除类目信息
     *
     * @param id 类目ID
     * @return 结果
     */
    @Override
    public int deleteMallAppCategoryById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }


    @Override
    public List<MallAppCategory> listPatent() {
        LambdaQueryWrapper<MallAppCategory> query = Wrappers.lambdaQuery(MallAppCategory.class);
        //query.eq(MallAppCategory::getPid, 0).orderByAsc(MallAppCategory::getSortOrder).orderByDesc(MallAppCategory::getSortUpdateTime);
        return baseMapper.selectList(query);
    }

    @Override
    public List<MallAppCategory> listChild(ReqIdParam reqIdParam) {
        LambdaQueryWrapper<MallAppCategory> query = Wrappers.lambdaQuery(MallAppCategory.class);
        //query.eq(MallAppCategory::getPid, reqIdParam.getId());
        query.orderByAsc(MallAppCategory::getSortOrder).orderByDesc(MallAppCategory::getSortUpdateTime);
        return baseMapper.selectList(query);
    }



    @Override
    public List<MallCategoryVO> listMedical() {
        List<MallAppCategory> list = lambdaQuery().eq(MallAppCategory::getStatus, 1).eq(MallAppCategory::getType,1)
                .orderByAsc(MallAppCategory::getSortOrder).orderByDesc(MallAppCategory::getSortUpdateTime)
                .list();
        return list.stream().map(item -> {
            MallCategoryVO mallCategoryVo = new MallCategoryVO();
            BeanUtils.copyProperties(item, mallCategoryVo);
            mallCategoryVo.setIconUrl(item.getIconUrl());
            return mallCategoryVo;
        }).collect(Collectors.toList());
    }




    @Override
    public List<MallCategoryVO> listFoods() {


        List<MallAppCategory> list = lambdaQuery().eq(MallAppCategory::getStatus, 1).eq(MallAppCategory::getType,0)
                .orderByAsc(MallAppCategory::getSortOrder).orderByDesc(MallAppCategory::getSortUpdateTime).list();


        return list.stream().map(item -> {
            MallCategoryVO mallCategoryVo = new MallCategoryVO();
            BeanUtils.copyProperties(item, mallCategoryVo);
            mallCategoryVo.setIconUrl(item.getIconUrl());
            return mallCategoryVo;
        }).collect(Collectors.toList());

    }

    /****
     * 上升
     * @param reqIdParam
     * @return
     */
    @Override
    public boolean reSortOrderReduce(MallAppCategory reqIdParam) {
        List<MallAppCategory> list = lambdaQuery().orderByAsc(MallAppCategory::getSortOrder)
                .orderByDesc(MallAppCategory::getSortUpdateTime).list();
        int indexOf = list.stream().map(MallAppCategory::getId).collect(Collectors.toList()).indexOf(reqIdParam.getId());
        if(indexOf == 0){
            return true;
        }
        MallAppCategory orgCategory = list.get(indexOf);
        MallAppCategory oldCategory = list.get(indexOf-1);
        Integer oldSortOrder = oldCategory.getSortOrder();
        Integer orgSortOrder = orgCategory.getSortOrder();
        orgCategory.setSortOrder(oldSortOrder);
        oldCategory.setSortOrder(orgSortOrder);
        updateById(orgCategory);
        updateById(oldCategory);
        return true;
    }
    /****
     * 下降
     * @param reqIdParam
     * @return
     */
    @Override
    public boolean reSortOrderPlus(MallAppCategory reqIdParam) {
//        List<MallAppCategory> list = lambdaQuery().eq(MallAppCategory::getPid,reqIdParam.getPid()).orderByAsc(MallAppCategory::getSortOrder)
        List<MallAppCategory> list = lambdaQuery().orderByAsc(MallAppCategory::getSortOrder)
                .orderByDesc(MallAppCategory::getSortUpdateTime).list();
        int indexOf = list.stream().map(MallAppCategory::getId).collect(Collectors.toList()).indexOf(reqIdParam.getId());
        if(indexOf == list.size()-1){
            return true;
        }
        MallAppCategory orgCategory = list.get(indexOf);
        MallAppCategory oldCategory = list.get(indexOf+1);
        Integer oldSortOrder = oldCategory.getSortOrder();
        Integer orgSortOrder = orgCategory.getSortOrder();
        orgCategory.setSortOrder(oldSortOrder);
        oldCategory.setSortOrder(orgSortOrder);
        updateById(orgCategory);
        updateById(oldCategory);

        return true;
    }
}
